package com.appkubes.mall.restful.application.goods.sku.controller;

import com.appkubes.common.Response;
import com.appkubes.mall.restful.application.common.base.BaseController;
import com.appkubes.mall.restful.application.goods.sku.entity.dto.RentOrderPeriodsDto;
import com.appkubes.mall.restful.application.goods.sku.entity.vo.SpecificCodeVo;
import com.appkubes.mall.restful.application.goods.sku.service.impl.GoodsSpecificServiceImpl;
import com.appkubes.mall.restful.common.constant.RentOrderPeriodCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Api(tags = "sku实例")
@RestController
@RequestMapping("specifictemplate")
public class GoodsSpecificController extends BaseController {

    @Autowired
    GoodsSpecificServiceImpl goodsSpecificServiceImpl;


    @ApiOperation("解析code")
    @PostMapping("/parseSpecificCode")
    public Response parseSpecificCode(@RequestBody @Valid SpecificCodeVo specificCodeVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Response.error("参数错误");
        }

        RentOrderPeriodCode rentOrderPeriodCode = RentOrderPeriodCode.valueOf(specificCodeVo.getPeriodsCode());

        RentOrderPeriodsDto rentOrderPeriodsDto = new RentOrderPeriodsDto();
        BeanUtils.copyProperties(rentOrderPeriodCode, rentOrderPeriodsDto);
        rentOrderPeriodsDto.setPeriodsDay(rentOrderPeriodCode.getPeriodDays());
        rentOrderPeriodsDto.setPeriodsType(rentOrderPeriodCode.getPeriodType());
        return Response.success(rentOrderPeriodsDto);
    }
}
